<template>
  <view class="driving-container">
    <u-navbar title="驾校报名" :autoBack="false"></u-navbar>
    
    <scroll-view 
      scroll-y 
      class="scroll-container"
      refresher-enabled="true"
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
      @scrolltolower="loadMore"
    >
      <view class="school-card" v-for="(school, index) in schoolList" :key="index" @click="goToDetail(school.id)">
        <image class="school-img" :src="school.image" mode="aspectFill"></image>
        <view class="school-info">
          <view class="school-title-row">
            <text class="school-name">{{ school.name }}</text>
          </view>
          <view class="school-address">{{ school.address }}</view>
          <view class="school-types">
            <text class="type-label">可报车型：</text>
            <text class="type-tag" v-for="(tag, tagIndex) in school.tags" :key="tagIndex">{{ tag }}</text>
          </view>
        </view>
      </view>
      
      <!-- 加载更多提示 -->
      <view class="load-more" v-if="schoolList.length > 0">
        <view v-if="loading" class="loading-text">加载中...</view>
        <view v-else-if="!hasNext" class="no-more-text">没有更多数据了</view>
        <view v-else class="load-more-text">上拉加载更多</view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { getSchoolList } from '@/src/api/driver/index.js'

export default {
  data() {
    return {
      refreshing: false,
      schoolList: [],
      // 分页相关
      currentPage: 1,
      pageSize: 5,
      hasNext: false,
      hasPrev: false,
      total: 0,
      totalPages: 0,
      loading: false
    }
  },
  onLoad() {
    this.loadSchoolList();
  },
  methods: {
    async loadSchoolList(isRefresh = false) {
      if (this.loading) return;
      
      this.loading = true;
      try {
        const response = await getSchoolList(this.currentPage, this.pageSize);
        if (response.code === 200) {
          if (isRefresh) {
            this.schoolList = response.data;
          } else {
            this.schoolList = [...this.schoolList, ...response.data];
          }
          
          // 更新分页信息
          this.hasNext = response.pagination.hasNext;
          this.hasPrev = response.pagination.hasPrev;
          this.total = response.pagination.total;
          this.totalPages = response.pagination.totalPages;
        } else {
          uni.showToast({
            title: response.message || '获取数据失败',
            icon: 'none'
          });
        }
      } catch (error) {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
      } finally {
        this.loading = false;
      }
    },
    
    async onRefresh() {
      this.refreshing = true;
      this.currentPage = 1;
      
      try {
        await this.loadSchoolList(true);
        uni.showToast({
          title: '刷新成功',
          icon: 'success'
        });
      } catch (error) {
        uni.showToast({
          title: '刷新失败',
          icon: 'none'
        });
      } finally {
        this.refreshing = false;
      }
    },
    
    async loadMore() {
      if (this.loading || !this.hasNext) return;
      
      this.currentPage++;
      await this.loadSchoolList();
    },
    
    goToDetail(schoolId) {
      uni.navigateTo({
        url: `/driver/detail/index?id=${schoolId}`
      });
    }
  }
}
</script>

<style scoped lang="scss">
.driving-container {
  background: #f5f5f5;
  min-height: 100vh;
  padding: 30rpx 0;
  padding-top: 180rpx;
  margin-top: 20rpx;
  margin-bottom: 20rpx;
  box-sizing: border-box;
}

.scroll-container {
  height: calc(100vh - 180rpx - 20rpx);
}

.school-card {
  background: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.06);
  margin: 0 30rpx 30rpx 30rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.school-img {
  width: 100%;
  height: 300rpx;
  object-fit: cover;
  border-top-left-radius: 16rpx;
  border-top-right-radius: 16rpx;
}

.school-info {
  padding: 24rpx 20rpx 20rpx 20rpx;
}

.school-title-row {
  display: flex;
  align-items: center;
  margin-bottom: 12rpx;
}

.school-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #222;
}



.school-address {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.school-types {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.type-label {
  font-size: 24rpx;
  color: #888;
}

.type-tag {
  font-size: 22rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 8rpx;
  padding: 4rpx 16rpx;
  margin-right: 8rpx;
}



.rating {
  display: flex;
  align-items: center;
  font-size: 24rpx;
  color: #666;
  
  text {
    margin-left: 6rpx;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30rpx 0;
}

.loading-text,
.no-more-text,
.load-more-text {
  font-size: 24rpx;
  color: #999;
}
</style> 